import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.UIManager;

import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import java.awt.Color;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.ImageIcon;


public class StudentServices extends JFrame {

	/**
	 * Create the panel.
	 */
	public StudentServices(final String user) {
		getContentPane().setBackground(Color.ORANGE);
		getContentPane().setLayout(null);
		
		JButton btnRegisterForCourses = new JButton("Register For Courses");
		btnRegisterForCourses.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
				new RegisterCourse(user);
			}
		});
		btnRegisterForCourses.setBounds(130, 35, 205, 23);
		getContentPane().add(btnRegisterForCourses);
		
		
		JButton btnUpdatePersonalInformation = new JButton("Update Personal Information");
		btnUpdatePersonalInformation.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			new StuPersonalInfo(user);
			}
		});
		btnUpdatePersonalInformation.setBounds(130, 84, 205, 23);
		getContentPane().add(btnUpdatePersonalInformation);
		
		JButton btnFindTutors = new JButton("Find Tutors");
		btnFindTutors.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
				new FindTutors(user);
			}
		});
		btnFindTutors.setBounds(130, 134, 205, 23);
		getContentPane().add(btnFindTutors);
		
		JButton btnNewButton = new JButton("Tutor Logbook");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
				new TutorLogbook(user);
			}
		});
		btnNewButton.setBounds(130, 182, 205, 23);
		getContentPane().add(btnNewButton);
		
		JButton btnViewGradingPattern = new JButton("View Grading Pattern");
		btnViewGradingPattern.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try{
					Connection con =Connect.getConnection();
					Statement stat = con.createStatement();
					UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

				    UIManager.put("OptionPane.background", Color.ORANGE);
					String nam = JOptionPane.showInputDialog (getContentPane(),"<html><b>Enter Instructor Name:</b></html>");
					ResultSet rs= stat.executeQuery("SELECT R.Grade, S.Course_Title,F.Name FROM FACULTY AS F, SECTION AS S, REGISTERS AS R WHERE F.Name='"+nam+"' AND F.Username=S.Instructor_Username AND S.CRN=R.Section_CRN AND Grade<>'INCOMPLETE' AND Grade_Mode='GRADE'");
					double avg=0.0;
					String savg="-";
					double total=0;
					double cum=0;
					String gr="";
					String course_title="";
					String name="";
					Boolean check=false;
					while(rs.next()){
						check=true;
						gr=rs.getString(1);
						if(gr.equals("A")){
							cum+=4;
						}else if(gr.equals("B")){
							cum+=3;
						}else if(gr.equals("C")){
							cum+=2;
						}else if(gr.equals("D")){
							cum+=1;
						}
						name=rs.getString(3);
						course_title=rs.getString(2);
						total+=4;
						
				    }
					if(check){
						avg=(cum/total) *4;
						savg=avg+"";
						check=false;
						}
					Icon i=new ImageIcon(FacultyServices.class.getResource("/resources/p-331002-georgia-tech-yellow-jackets-black-spare-tire-cover-jt-2324558420.jpg"));
					JOptionPane.showMessageDialog(getContentPane(),"Instructor Name: '"+name +"'\n Course Title: '" + course_title +"'\n Average GPA: '" + avg +"'","Instructor Average Grade",JOptionPane.INFORMATION_MESSAGE,i);
				}catch(Exception e){
					JOptionPane.showMessageDialog(getContentPane(),"Check Connection");
				}
				
			}
		});
		btnViewGradingPattern.setBounds(130, 235, 205, 23);
		getContentPane().add(btnViewGradingPattern);
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setBounds(0, 0, 440, 48);
		getContentPane().add(lblNewLabel);
		
		JLabel label = new JLabel("");
		label.setIcon(new ImageIcon(StudentServices.class.getResource("/resources/p-331002-georgia-tech-yellow-jackets-black-spare-tire-cover-jt-2324558420.jpg")));
		label.setBounds(385, 6, 280, 213);
		getContentPane().add(label);
		
		JButton btnLogout = new JButton("LOGOUT");
		btnLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
				new LogIn();
			}
		});
		btnLogout.setBounds(400, 232, 117, 29);
		getContentPane().add(btnLogout);
		setPreferredSize(new Dimension(700, 300));
		pack();
		setVisible(true);
		setTitle("Student Services");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}
}
